<?php
namespace app\api\controller;
use think\Db;
use app\admin\model\Config;

//会员相关
class MemberWith extends App
{
    
    //收藏院校
    public function school_collect()
    {
        $user_id = $this->user_id;
        if($this->request->isPost()){
			
			$school_id = $this->request->post("school_id",0,'intval');
			if(empty($school_id)){
				return $this->show(2001,"缺少参数school_id!", '');
			}
			$flag = Db::name("school_collect")->where('user_id',$user_id)->where('school_id',$school_id)->value('id');
			$data['school_id'] = $school_id;
			$data['user_id'] = $user_id;
			if(empty($flag)){
				$data['create_time'] = time();
				$res = Db::name("school_collect")->insert($data);
			}else{
				$data['update_time'] = time();
				$res = Db::name("school_collect")->where('id',$flag)->update($data);
			}
			if( $res !== false ){
				return $this->show(1,"success", '');
			}else{
				return $this->show(2002, "fail", "");
			} 
		}else{
            return $this->show(2001, "非法请求", "");
        }
    }
    
	//取消收藏院校
    public function school_cancel()
    {
        $user_id = $this->user_id;
        $school_id = $this->request->get("school_id",0,'intval'); 
		if(empty($school_id)){
			return $this->show(2001,"缺少参数school_id!", '');
		}
		
		$flag = Db::name("school_collect")->where('user_id',$user_id)->where('school_id',$school_id)->delete();
		return $this->show(1,"success", '');
    }
    
    
    //收藏专业
    public function major_collect()
    {
        $user_id = $this->user_id;
        if($this->request->isPost()){
			
			$major_id = $this->request->post("major_id",0,'intval');
			if(empty($major_id)){
				return $this->show(2001,"缺少参数major_id!", '');
			}
			$flag = Db::name("major_collect")->where('user_id',$user_id)->where('major_id',$major_id)->value('id');
			$data['major_id'] = $major_id;
			$data['user_id'] = $user_id;
			if(empty($flag)){
				$data['create_time'] = time();
				$res = Db::name("major_collect")->insert($data);
			}else{
				$data['update_time'] = time();
				$res = Db::name("major_collect")->where('id',$flag)->update($data);
			}
			if( $res !== false ){
				return $this->show(1,"success", '');
			}else{
				return $this->show(2002, "fail", "");
			} 
		}else{
            return $this->show(2001, "非法请求", "");
        }
    }
    
	//取消收藏专业
    public function major_cancel()
    {
        $user_id = $this->user_id;
        $major_id = $this->request->get("major_id",0,'intval'); 
		if(empty($major_id)){
			return $this->show(2001,"缺少参数major_id!", '');
		}
		
		$flag = Db::name("major_collect")->where('user_id',$user_id)->where('major_id',$major_id)->delete();
		return $this->show(1,"success", '');
    }
	
	
    //收藏资讯
    public function article_collect()
    {
        $user_id = $this->user_id;
        if($this->request->isPost()){
			
			$article_id = $this->request->post("article_id",0,'intval');
			if(empty($article_id)){
				return $this->show(2001,"缺少参数article_id!", '');
			}
			$flag = Db::name("article_collect")->where('user_id',$user_id)->where('article_id',$article_id)->value('id');
			$data['article_id'] = $article_id;
			$data['user_id'] = $user_id;
			if(empty($flag)){
				$data['create_time'] = time();
				$res = Db::name("article_collect")->insert($data);
			}else{
				$data['update_time'] = time();
				$res = Db::name("article_collect")->where('id',$flag)->update($data);
			}
			if( $res !== false ){
				return $this->show(1,"success", '');
			}else{
				return $this->show(2002, "fail", "");
			} 
		}else{
            return $this->show(2001, "非法请求", "");
        }
    }
    
	//取消收藏资讯
    public function article_cancel()
    {
        $user_id = $this->user_id;
        $article_id = $this->request->get("article_id",0,'intval'); 
		if(empty($article_id)){
			return $this->show(2001,"缺少参数article_id!", '');
		}
		
		$flag = Db::name("article_collect")->where('user_id',$user_id)->where('article_id',$article_id)->delete();
		return $this->show(1,"success", '');
    }
    
}